/*
 *  hackfs/course_design/voldemort_language/src/record.c
 *
 *  (C) 2011 Harry Wei
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>

#include "../include/vl.h"

int 
record(char *vol_lan, struct mean *implication, int i_counter)
{
	int i = 0;
	int fd;
	int counter = 0;
	char ch = ' ';

	if (0 > (fd = creat("result", 0644))) {
		fprintf(stderr, "Creat File Error!\n");
		return 1;
	}
	while (vol_lan[i]) {
		for (counter = 0; counter < i_counter; counter++) {
			if ((vol_lan[i] - 1) == implication[counter].lower_ch) {
				if (strlen(implication[counter].meaning) != 
					write(fd, implication[counter].meaning, 
						strlen(implication[counter].meaning))) {
					fprintf(stderr, "Write Error!\n");
					return 1;
				}
				if (sizeof(char) != write(fd, &ch, sizeof(char))) {
					fprintf(stderr, "Write Error!\n");
					return 1;
				}
			}
		}
		i += 1;
	}
	if (close(fd)) {
		fprintf(stderr, "Close Error!\n");
		return 1;
	}
	return 0;
}
